using Identity.Domain.Aggregates;
using Identity.Domain.Exceptions;
using Identity.Domain.ValueObjects;

namespace Identity.Infrastructure.Persistence.Entities
{
    public class ApiPermissionBinding
    {
       
        public string ResourceId { get; private set; }
        public Guid? permissionId { get; private set; }
        public Permission? Permission { get; private set; }
        public string FromServiceName { get; private set; }
        public string Description { get; private set; }
        public bool IsActive { get; private set; }

        public PermissionScope PermissionScope { get; private set; }

        public ApiPermissionBinding() { }

        public static ApiPermissionBinding Create(string resourceId, string description, string fromServiceName, PermissionScope permissionScope)
        {

            return new ApiPermissionBinding
            {
               
                ResourceId = resourceId,
                Description = description,
                FromServiceName = fromServiceName,
                IsActive=false,
                PermissionScope = permissionScope
            };

        }

        public void ChangePermissionScope(PermissionScope permissionScope)
        {
            if (PermissionScope == permissionScope) return;
            PermissionScope = permissionScope;
        }
        public void ChangeActive(bool isActive)
        {
            if (IsActive == isActive) return;
            IsActive = isActive;
        }
        public void AssignPermission(Guid permissionId)
        {
            if (this.permissionId != null) throw new DomainException("该接口已经绑定访问权限");
            this.permissionId = permissionId;
        }

        public void ChangePermissionBinding(Guid newPermission)
        {
            if (permissionId == newPermission) return;
            permissionId = newPermission;
        }

    }
}